package cd.cdyb.sms.utils;

import org.apache.commons.lang3.RandomUtils;

import java.util.List;


public final class RandomUtilPlus extends RandomUtils {

    private RandomUtilPlus() {

    }

    /**
     * 生成随机数，且生成的随机数不在排除的范围内
     *
     * @param startInclusive 开始值
     * @param endExclusive   结束值
     * @param exclusives     需要排除的值
     * @return 随机数
     */
    public static int nextInt(final int startInclusive, final int endExclusive, Integer... exclusives) {
        int i = nextInt(startInclusive, endExclusive);
        List<Integer> exclusiveList = CollectionUtilPlus.asList(exclusives);
        while (exclusiveList.contains(i)) {
            i = nextInt(startInclusive, endExclusive);
        }
        return i;
    }


    public static String generate() {

        char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z','0','1','2','3','4','5','6','7','8','9'};
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[6];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            } while (flags[index]);// 判断生成的字符是否重复
            chs[i] = letters[index];
            flags[index] = true;
        }
        return "a2" + String.valueOf(chs);
    }

    /**
     * 雪花算法生成编号
     *
     * @param prefix 前缀
     * @return 编号
     */
/*    public static String generateSnowflakeNumber(String prefix) {
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        String number = prefix + id;
        return number;
    }*/

    /**
     * 获取指定数量的随机数
     *
     * @param min 最小数
     * @param max 最大数
     * @param n 生成数
     * @return 指定数量的随机数
     */
    public static int[] randomCommon(int min, int max, int n) {
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while (count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }


    public static void main(String[] args) {

    }

}
